import { getResume } from "~/server/database/services/resumeService";
import authMiddleware from "~/server/middleware/auth";

export default defineEventHandler(async (event) => {
  try {
    // 使用认证中间件获取用户信息
    await authMiddleware(event);

    const id = getRouterParam(event, "id");
    console.log("useruseruseruseruseruseruseruseruseruseruseruseruseruseruser");

    // 从event.context中获取用户信息
    const user = event.context.user;
    console.log(user.id, "useruseruseruseruser");

    if (!id) {
      throw createError({
        statusCode: 400,
        statusMessage: "简历ID是必需的",
      });
    }

    const resume = await getResume(parseInt(id));

    if (!resume) {
      throw createError({
        statusCode: 404,
        statusMessage: "简历不存在",
      });
    }

    // 检查简历是否属于当前用户
    if (resume.dataValues.userId !== user.id) {
      throw createError({
        statusCode: 403,
        statusMessage: "无权访问此简历",
      });
    }

    return {
      success: true,
      data: resume.dataValues,
    };
  } catch (error: any) {
    console.error("获取简历详情失败:", error);
    if (error.statusCode) {
      throw error;
    }
    throw createError({
      statusCode: 500,
      statusMessage: "获取简历详情失败",
    });
  }
});
